package br.com.vfsb.controller;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.mindmap.DefaultMindmapNode;
import org.primefaces.model.mindmap.MindmapNode;
 
@ManagedBean(name = "mindmapView")
public class ControllerMindmap implements Serializable {
     
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private MindmapNode root;
     
    private MindmapNode selectedNode;
     
    public ControllerMindmap() {
        root = new DefaultMindmapNode("Upload", "Google WebSite", "FF3366", false);
         
        MindmapNode ips = new DefaultMindmapNode("BI", "IP Numbers", "C8C8C8", true);
        MindmapNode ns = new DefaultMindmapNode("BF", "Namespaces", "C8C8C8", true);
        MindmapNode malware = new DefaultMindmapNode("XLS", "Malicious Software", "C8C8C8", true);
         
        root.addNode(ips);
        root.addNode(ns);
        root.addNode(malware);
        
    }
 
    public MindmapNode getRoot() {
        return root;
    }
 
    public MindmapNode getSelectedNode() {
        return selectedNode;
    }
    public void setSelectedNode(MindmapNode selectedNode) {
        this.selectedNode = selectedNode;
    }
 
    public void onNodeSelect(SelectEvent event) {
        MindmapNode node = (MindmapNode) event.getObject();
         
        //populate if not already loaded
        if(node.getChildren().isEmpty()) {
            Object label = node.getLabel();
 
            if(label.equals("NS(s)")) {
                for(int i = 0; i < 30; i++) {
                    node.addNode(new DefaultMindmapNode("ns" + i + ".google.com", "Namespace " + i + " of Google", "82c542", false));
                }
            }
            else if(label.equals("IPs")) {
                for(int i = 0; i < 20; i++) {
                    node.addNode(new DefaultMindmapNode("1.1.1."  + i, "IP Number: 1.1.1." + i, "#C8C8C8", false));
                } 
            }
            else if(label.equals("Malware")) {
                for(int i = 0; i < 20; i++) {
                    //String random = UUID.randomUUID().toString();
                    //node.addNode(new DefaultMindmapNode("Malware-"  + random, "Malicious Software: " + random, "3399ff", false));
                    node.addNode(new DefaultMindmapNode("Malware-"  + i, "Malicious Software: " + i, "#C8C8C8", false));
                }
            }
        }   
    }
     
    public void onNodeDblselect(SelectEvent event) {
        this.selectedNode = (MindmapNode) event.getObject();  
        
    }
}
